package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.dao.ClassCourseDao;
import com.example.domain.ClassCourse;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.ClassCourseService;
import com.example.tool.DateTool;

@Service
public class ClassCourseServiceImpl implements ClassCourseService {

    @Autowired
    ClassCourseDao classCourseDao;

    /**
     * @describe 添加班级课程信息
     */
    @Override
    public Result addClassCourse(String json) {
        JSONObject parseObject = JSON.parseObject(json);
        String[] courseId = parseObject.getString("courseId").split(",");
        String[] classId = parseObject.getString("classId").split(",");
        List<ClassCourse> list = new ArrayList<ClassCourse>();

        if (courseId != null && classId != null) {
            for (int i = 0; i < courseId.length; i++) {
                ClassCourse classCourse = new ClassCourse();
                classCourse.setCourseId(Integer.parseInt(courseId[i]));
                classCourse.setClassId(Integer.parseInt(classId[0]));
                // 获取系统当前时间作为记录创建时间
                classCourse.setRecordTime(DateTool.getTimestamp());
                list.add(classCourse);
            }

        }
        // 调用JPA保存实体方法
        List<ClassCourse> classCourse1 = classCourseDao.saveAll(list);

        if (classCourse1 != null) {
            return ResultFactory.buildSuccessResult(classCourse1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新班级课程信息
     */
    @Override
    public Result updateClassCourse(ClassCourse classCourse) {
        if (classCourse != null) {
            // 获取系统当前时间作为记录更新时间
            classCourse.setUpdateTime(DateTool.getTimestamp());
        }
        // 调用JPA保存并刷新实体方法
        ClassCourse classCourse1 = classCourseDao.saveAndFlush(classCourse);

        if (classCourse1 != null) {
            return ResultFactory.buildSuccessResult(classCourse1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据班级课程编号删除班级课程信息
     */
    @Override
    public Result deleteClassCourseById(String classCourseId) {
        int row = classCourseDao.deleteByClassCourseId(Integer.parseInt(classCourseId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有班级课程信息
     */
    @Override
    public Result queryAllClassCourse() {
        List<ClassCourse> list = classCourseDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号查询班级课程信息
     */
    @Override
    public Result queryClassCourseById(String classCourseId) {
        List<ClassCourse> list = classCourseDao.findByClassCourseId(Integer.parseInt(classCourseId));
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加班级课程信息
     */
    @Override
    public Result addClassCourseByExcel(List<ClassCourse> list) {
        List<ClassCourse> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = classCourseDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据班级课程编号批量删除班级课程信息
     */
    @Override
    public Result deleteClassCourseByIds(Map<String, String> ids) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = classCourseDao.deleteByClassCourseIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 班级课程信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<Map<String, Object>> list = null;

        if (info[0].equals("termName")) {

            list = classCourseDao.findByTermNameLike(info[1]);

        } else if (info[0].equals("className")) {

            list = classCourseDao.findByClassNameLike(info[1]);

        } else if (info[0].equals("courseName")) {

            list = classCourseDao.findByCourseNameLike(info[1]);

        }

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有未分配的班级名称
     */
    @Override
    public Result queryAllClassNameOfNotAllocation() {
        List<Map<String, Object>> list = classCourseDao.queryAllClassNameOfNotAllocation();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有课程和班级的已分配信息
     */
    @Override
    public Result queryAllCourseAndClassInfo() {
        List<Map<String, Object>> list = classCourseDao.queryAllCourseAndClassInfo();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }


}
